package net.lgpage.http;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.lgpage.http.ExceptionAsyncTask.HttpErrorCodeException;
import net.lgpage.http.ExceptionAsyncTask.HttpMsgPauseException;
import net.lgpage.http.ExceptionAsyncTask.HttpSessionOutTimeException;
import net.lgpage.http.Page.Pageable;
import net.lgpage.tool.GsonTool;

import org.json.JSONArray;
import org.json.JSONObject;

public class ServiceUtil2 {

	public static final String POST = "POST";
	public static final String GET = "GET";
	
	public static <T> T getObjFromHttpMsg(String appurl,String method,
			Object obj, Class<T> clazz) throws Exception {
		String res = HttpConnectionUtil.requestAppendObj(appurl,method,obj);
		HttpMsg msg = checkHttpMsg(res);
		T tmps = null;
		tmps = GsonTool.getGson().fromJson(msg.getObj().toString(), clazz);
		return tmps;
	}
	
	public static <T> T getObjByMapFromHttpMsg(String appurl,String method,
			Map<String, String> map, Class<T> clazz)throws Exception{
		String res = HttpConnectionUtil.requestAppendMap(appurl, method, map);
		HttpMsg msg = checkHttpMsg(res);
		T tmps = null;
		if(clazz ==String.class)return (T) msg.getObj().toString();
		if(clazz ==Integer.class){
			Integer ri = Integer.parseInt(msg.getObj().toString());
			return (T) ri;
		}
		System.out.println(msg.getObj().toString());
		tmps = GsonTool.getGson().fromJson(msg.getObj().toString(), clazz);
		return tmps;
	}
	
	public static <T> List<T>getListByMapFromHttpMsg(String appurl,String method,
			Map<String, String> map, Class<T> clazz)throws Exception{
		String res = HttpConnectionUtil.requestAppendMap(appurl, method, map);
		HttpMsg msg = checkHttpMsg(res);
		//List<T> list = gson.fromJson(msg.getObj(), new TypeToken<List<T>>(){}.getType()); 
		List<T> list = GsonTool.getList(msg.getObj().toString(), clazz);
		return list;
	}
	
	public static <T> List<T> getListFromHttpMsg(String appurl,String method,
			Object obj, Class<T> clazz) throws Exception {
		String res = HttpConnectionUtil.requestAppendObj(appurl, method, obj);
		HttpMsg msg = checkHttpMsg(res);
		System.out.println(msg.getObj().toString());
		//List<T> list = gson.fromJson(msg.getObj(), new TypeToken<List<T>>(){}.getType()); 
		List<T> list = GsonTool.getList(msg.getObj().toString(), clazz);
		return list;
	}

	public static <T> List<T> getListFromHttpMsg(String appurl,String method,
			String data, Class<T> clazz) throws Exception {
		String res = HttpConnectionUtil.requestAppendData(appurl, method, data);
		HttpMsg msg = checkHttpMsg(res);
		System.out.println(msg.getObj().toString());
		//List<T> list = gson.fromJson(msg.getObj(), new TypeToken<List<T>>(){}.getType()); 
		List<T> list = GsonTool.getList(msg.getObj().toString(), clazz);
		return list;
	}
	/*public static <T> Page<T> getPageListFromHttpMsg(String appurl,String method,
			Object obj, Class<T> clazz) throws Exception {
		String res = HttpConnectionUtil.requestAppendObj(appurl, method, obj);
		HttpMsg msg = checkHttpMsg(res);
		JSONObject jo = new JSONObject(msg.getObj());
		JSONArray ja = jo.getJSONArray("content");
		JSONObject pagejo = jo.getJSONObject("pageable");
		Pageable pageable = GsonTool.getGson().fromJson(pagejo.toString(), Pageable.class);
		Integer total = jo.getInt("total");
		List<T>list = new ArrayList<T>();
		for(int i = 0;i<ja.length();i++){
			JSONObject jo2 = ja.getJSONObject(i);
			T t = GsonTool.getGson().fromJson(jo2.toString(), clazz);
			list.add(t);
		}
		Page<T> page = new Page<T>();
		page.setList(list);
		page.setTotal(total);
		page.setPageable(pageable);
		return page;
	}*/
	
	public static HttpMsg getHttpMsg(String appurl,String method, Object obj)
			throws Exception {
		String res = HttpConnectionUtil.requestAppendObj(appurl, method, obj);
		return checkHttpMsg(res);
	}
	public static HttpMsg getHttpMsg(String appurl,String method, Map<String, String> map)
			throws Exception {
		String res = HttpConnectionUtil.requestAppendMap(appurl, method, map);
		return checkHttpMsg(res);
	}
	
	public static HttpMsg putObj(String appurl,String method, Object obj)
			throws Exception {
		String res = HttpConnectionUtil.requestAppendObj(appurl, method, GsonTool.getGson().toJson(obj));
		return checkHttpMsg(res);
	}
	public static HttpMsg checkHttpMsg(String res) throws Exception {
		JSONObject jo = new JSONObject(res);
		HttpMsg msg = new HttpMsg();
		try {
			boolean statu = jo.getBoolean("statu");
			msg.setStatu(statu);
		} catch (Exception e2) {
		}
		try {
			String errormsg = jo.getString("msg");
			msg.setMsg(errormsg);
		} catch (Exception e1) {
		}
		try {
			Object data = jo.get("data");
			msg.setData(data);
		} catch (Exception e) {
		}
		
		
		//HttpMsg msg = GsonTool.getGson().fromJson(res, HttpMsg.class);
		if (msg == null) {
			throw new HttpMsgPauseException();
		} else {
			if (1 == msg.getStatusCode()) {
				return msg;
			} else if("0".equals(msg.getErrmsg())){
				throw new HttpSessionOutTimeException(msg.getErrmsg());
			}else {
				throw new HttpErrorCodeException(msg.getErrmsg());
			}
		}
	}
	
}
